package com.xunqi.ad.advice;

import com.xunqi.ad.enums.ResponseEnum;
import com.xunqi.ad.exception.AdException;
import com.xunqi.ad.vo.ResponseVo;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: 统一异常处理
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @create: 2020-04-28 17:16
 **/
@ControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = AdException.class)
    @ResponseBody
    //@ResponseStatus(HttpStatus.FORBIDDEN)   //返回指定HTTP状态码给前端
    public ResponseVo handlerAdException(AdException exception) {

        // CommonResponseVo<String> responseVo = new CommonResponseVo<>(-1,"business error");
        // responseVo.setData(exception.getMessage());

        return ResponseVo.error(ResponseEnum.ERROR,exception.getMessage());
    }


    /**
     * 参数非法（效验参数）异常
     * @param exception
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ResponseVo methodHandler(MethodArgumentNotValidException exception) {
        return ResponseVo.error(ResponseEnum.PARAM_ERROR);
    }

}
